package soccerscope.dialog;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import soccerscope.model.ClientScene;
import soccerscope.model.PlayMode;
import soccerscope.model.WorldModel;
import soccerscope.model.PlayMode.PLAY_MODE;
import soccerscope.training.conditions.BallPossessionCondition;
import soccerscope.training.conditions.CycleCondition;
import soccerscope.training.conditions.GameCondition;
import soccerscope.training.conditions.PlayModeCondition;

/**
 * This code was generated using CloudGarden's Jigloo SWT/Swing GUI Builder,
 * which is free for non-commercial use. If Jigloo is being used commercially
 * (ie, by a corporation, company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo. Please visit
 * www.cloudgarden.com for details. Use of Jigloo implies acceptance of these
 * licensing terms. ************************************* A COMMERCIAL LICENSE
 * HAS NOT BEEN PURCHASED for this machine, so Jigloo or this code cannot be
 * used legally for any corporate or commercial purpose.
 * *************************************
 */
public class GameConditionPanel
    extends JPanel
    implements ActionListener, ListSelectionListener, ChangeListener {

  private static final long       serialVersionUID = 1L;

  private JPanel                  playMode;

  private JRadioButton            rightTeam;

  private ButtonGroup             ballPossGroup;

  private JRadioButton            leftTeam;

  // private JSpinner cycleSpinner;
  private JTextField              cycleField;

  private JList                   pmList;

  private JScrollPane             pmScrollPane;

  private JCheckBox               activateBPoss;

  private JPanel                  ballPoss;

  private JCheckBox               activeCycles;

  private JCheckBox               activePlayMode;

  private JPanel                  cycle;

  private GameCondition[]         gc;

  private PlayModeCondition       pmCond;

  private CycleCondition          cycleCond;

  private BallPossessionCondition ballPossCond;

  // private String[] values;

  public GameConditionPanel(
      ClientScene scene) {

    super();
    this.gc = WorldModel.getInstance().trainModel.gameCondition;
    this.initGUI();
    this.setElements();
  }

  private void setElements() {

    this.gc = WorldModel.getInstance().trainModel.gameCondition;
    for (int i = 0; i < this.gc.length; i++) {
      if (this.gc[i] instanceof PlayModeCondition) {
        this.pmCond = (PlayModeCondition) this.gc[i];
        this.activePlayMode.setSelected(this.gc[i].isActive());
        this.pmList.setEnabled(this.gc[i].isActive());
        PLAY_MODE[] selected = (PLAY_MODE[]) this.pmCond.getPlayModes();
        if (selected != null) {
          int[] indices = new int[selected.length];
          for (int pm = 0; pm < selected.length; pm++) {
            indices[pm] = PlayMode.getIndexFor(selected[pm]);
          }
          this.pmList.setSelectedIndices(indices);
        }
      }
      else if (this.gc[i] instanceof CycleCondition) {
        this.cycleCond = (CycleCondition) this.gc[i];
        this.activeCycles.setSelected(this.gc[i].isActive());
        // cycleSpinner.setEnabled(gc[i].isActive());
        // cycleSpinner.setValue(values[cycleCond.getCycles()-10]);
        this.cycleField.setEnabled(this.gc[i].isActive());
        this.cycleField.setText("" + this.cycleCond.getCycles());

      }
      else if (this.gc[i] instanceof BallPossessionCondition) {
        this.ballPossCond = (BallPossessionCondition) this.gc[i];
        this.activateBPoss.setSelected(this.gc[i].isActive());
        this.leftTeam.setEnabled(this.gc[i].isActive());
        this.rightTeam.setEnabled(this.gc[i].isActive());
        boolean left = this.ballPossCond.isLeftTeam();
        if (left) {
          this.leftTeam.setSelected(true);
        }
        else {
          this.rightTeam.setSelected(true);
        }
      }
    }

  }

  private void initGUI() {

    try {
      GridBagLayout thisLayout = new GridBagLayout();
      thisLayout.columnWeights = new double[] { 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 };
      thisLayout.columnWidths = new int[] { 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 };
      thisLayout.rowWeights = new double[] { 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 };
      thisLayout.rowHeights = new int[] { 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 };
      this.setLayout(thisLayout);
      {
        {
          this.ballPossGroup = new ButtonGroup();
        }
        this.playMode = new JPanel();
        GridLayout playModeLayout = new GridLayout(1, 1);
        this.playMode.setLayout(playModeLayout);
        this.add(this.playMode, new GridBagConstraints(0, 1, 2, 2, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,
                0, 0, 0), 0, 0));
        this.playMode.setEnabled(false);
        this.playMode.setBorder(BorderFactory.createTitledBorder(BorderFactory
            .createEtchedBorder(BevelBorder.LOWERED), "PlayMode",
            TitledBorder.LEADING, TitledBorder.TOP));
        {
          this.pmScrollPane = new JScrollPane();
          this.playMode.add(this.pmScrollPane);
          {
            DefaultListModel pmListModel = new DefaultListModel();
            for (PlayMode.PLAY_MODE p : PlayMode.allPlayModes) {
              pmListModel.addElement(p);
            }
            this.pmList = new JList();
            this.pmScrollPane.setViewportView(this.pmList);
            this.pmList.setModel(pmListModel);
            this.pmList.setVisibleRowCount(5);
            this.pmList.addListSelectionListener(this);
          }
        }
      }
      {
        this.cycle = new JPanel();
        FlowLayout cycleLayout = new FlowLayout();
        cycleLayout.setAlignment(FlowLayout.LEFT);
        this.cycle.setLayout(cycleLayout);
        this.add(this.cycle, new GridBagConstraints(2, 1, 2, 2, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,
                0, 0, 0), 0, 0));
        this.cycle.setBorder(BorderFactory.createTitledBorder(BorderFactory
            .createEtchedBorder(BevelBorder.LOWERED), "Stop After",
            TitledBorder.LEADING, TitledBorder.TOP));
        {

          // values = new String[100];
          // for (int i = 0; i < values.length; i++)
          // values[i] = "" + (i+10);
          // SpinnerListModel cycleSpinnerModel = new
          // SpinnerListModel(values);

          // cycleSpinner = new JSpinner();
          // cycle.add(cycleSpinner);
          // cycleSpinner.setModel(cycleSpinnerModel);
          // cycleSpinner.setMinimumSize(new java.awt.Dimension(70,
          // 20));
          // cycleSpinner.setSize(80, 20);
          // cycleSpinner.addChangeListener(this);
          this.cycleField = new JTextField();
          this.cycle.add(this.cycleField);
          this.cycleField.setColumns(4);
          this.cycleField.addActionListener(this);
        }
      }
      {
        this.activePlayMode = new JCheckBox();
        this.add(this.activePlayMode, new GridBagConstraints(0, 0, 2, 1, 0.0,
            0.0, GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE,
            new Insets(3, 3, 3, 3), 0, 0));
        this.activePlayMode.setText("Activate");
        this.activePlayMode.addActionListener(this);
      }
      {
        this.activeCycles = new JCheckBox();
        this.add(this.activeCycles, new GridBagConstraints(2, 0, 2, 1, 0.0,
            0.0, GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE,
            new Insets(3, 3, 3, 3), 0, 0));
        this.activeCycles.setText("Activate");
        this.activeCycles.addActionListener(this);
      }
      {
        this.ballPoss = new JPanel();
        BoxLayout ballPossLayout = new BoxLayout(this.ballPoss,
            javax.swing.BoxLayout.Y_AXIS);
        this.ballPoss.setLayout(ballPossLayout);
        this.add(this.ballPoss, new GridBagConstraints(4, 1, 2, 2, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,
                0, 0, 0), 0, 0));
        this.ballPoss.setBorder(BorderFactory.createTitledBorder(BorderFactory
            .createEtchedBorder(BevelBorder.LOWERED), "Ball-Possession",
            TitledBorder.LEADING, TitledBorder.TOP));
        {
          this.leftTeam = new JRadioButton();
          this.ballPoss.add(this.leftTeam);
          this.leftTeam.setText("Left Team");
          this.ballPossGroup.add(this.leftTeam);
          this.leftTeam.addActionListener(this);
        }
        {
          this.rightTeam = new JRadioButton();
          this.ballPoss.add(this.rightTeam);
          this.rightTeam.setText("Right Team");
          this.ballPossGroup.add(this.rightTeam);
          this.rightTeam.addActionListener(this);
        }
      }
      {
        this.activateBPoss = new JCheckBox();
        this.add(this.activateBPoss, new GridBagConstraints(4, 0, 2, 1, 0.0,
            0.0, GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE,
            new Insets(3, 3, 3, 3), 0, 0));
        this.activateBPoss.setText("Activate");
        this.activateBPoss.addActionListener(this);
      }
      this.setSize(641, 414);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * This method is invoked, when the checkboxes to activate the the conditions
   * are (de)checked.
   */
  public void actionPerformed(ActionEvent arg0) {

    if (arg0.getSource() == this.activateBPoss) {
      this.rightTeam.setEnabled(this.activateBPoss.isSelected());
      this.leftTeam.setEnabled(this.activateBPoss.isSelected());
      this.ballPossCond.setActive(this.activateBPoss.isSelected());
    }
    else if (arg0.getSource() == this.activeCycles) {
      this.cycleField.setEnabled(this.activeCycles.isSelected());
      this.cycleCond.setActive(this.activeCycles.isSelected());
    }
    else if (arg0.getSource() == this.activePlayMode) {
      this.pmList.setEnabled(this.activePlayMode.isSelected());
      this.pmCond.setActive(this.activePlayMode.isSelected());
    }
    else if (arg0.getSource() == this.leftTeam) {
      this.ballPossCond.setLeftTeam(true);
    }
    else if (arg0.getSource() == this.rightTeam) {
      this.ballPossCond.setLeftTeam(false);
    }
    else if (arg0.getSource() == this.cycleField) {
      String value = (String) this.cycleField.getText();
      try {
        int cycles = Integer.parseInt(value);
        this.cycleCond.setCycles(cycles);
      }
      catch (Exception e) {
        e.printStackTrace();
        this.cycleField.setText("" + this.cycleCond.getCycles());
      }
    }
  }

  /**
   * This method listens for events on the play-mode list!
   * 
   * @param arg0
   */
  public void valueChanged(ListSelectionEvent arg0) {

    // System.out.println(pmList.getSelectedValues());
    int[] indices = this.pmList.getSelectedIndices();
    PLAY_MODE[] pms = new PLAY_MODE[indices.length];
    for (int i = 0; i < indices.length; i++) {
      pms[i] = PlayMode.allPlayModes[indices[i]];
    }
    this.pmCond.setPlayModes(pms);
  }

  /**
   * This method listens to changes in the cycle-spinner
   * 
   * @param arg0
   */
  public void stateChanged(ChangeEvent arg0) {

    // String value = (String)cycleSpinner.getValue();
    // try {
    // int cycles = Integer.parseInt(value);
    // cycleCond.setCycles(cycles);
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
  }

  @Override
  public void setVisible(boolean aFlag) {

    this.setElements();
    super.setVisible(aFlag);
  }

}
